<?php
/**
 * 此文件为超级装载器
 *
 */

define('SP_LOADER', '__SP__LOADER_CORE__');

$GLOBALS[SP_LOADER] = array(
    'SELF'                => null,
    'OBJECTS'             => array(),
    'CONFIGS'             => array(),
    'HELPERS'             => array(),
    'MODELS'              => array(),
    'DATABASES'           => array()
);

class Loader{
    function &instance(){
        if(!isset($GLOBALS[SP_LOADER]['SELF'])) $GLOBALS[SP_LOADER]['SELF'] =& new Loader();
        return $GLOBALS[SP_LOADER]['SELF'];
    }
        /**
         * 装载库文件
         */
    function &lib($class){
        $class = ucfirst($class);
        if (isset($GLOBALS[SP_LOADER]['OBJECTS'][$class])){
            return $GLOBALS[SP_LOADER]['OBJECTS'][$class];
        }
        if(file_exists(APPBASE.'Libraries/MY_'.$class.'.php')){
            require(APPBASE.'Libraries/MY_'.$class.'.php');
            $name = 'MY_'.$class;
            $GLOBALS[SP_LOADER]['OBJECTS'][$class] =& new $name();
            return $GLOBALS[SP_LOADER]['OBJECTS'][$class];
        }else{
            if (file_exists(SYSBASE.'Libraries/SP_'.$class.'.php')){
                require(SYSBASE.'Libraries/SP_'.$class.'.php');
                $name = 'SP_'.$class;
                $GLOBALS[SP_LOADER]['OBJECTS'][$class] =& new $name();
                return $GLOBALS[SP_LOADER]['OBJECTS'][$class];
            }else{
                showError(sprintf(__T("Class SP_%s is not valid!"),$class));
            }
        }
    }
    /**
     * 装载模型
     */
    function &model($modelName){
        if(!isset($GLOBALS[SP_LOADER]['MODELS'][$modelName])){
            $modelClass = $modelName.'Model';
            if(defined("MODELDIR") && MODELDIR){
                 if(file_exists(MODELDIR.$modelClass.'.php')) {
                     require(MODELDIR.$modelClass.'.php');
                     $GLOBALS[SP_LOADER]['MODELS'][$modelName] =& new $modelClass;
                     return $GLOBALS[SP_LOADER]['MODELS'][$modelName];
                 }
            }
            $modelPath = APPBASE.'Models/'.$modelClass.'.php';
            if(file_exists($modelPath)) {
                require($modelPath);
            }else{ 
                showError('Model '.$modelName.' don\'t exist!');
            }

            $GLOBALS[SP_LOADER]['MODELS'][$modelName] =& new $modelClass;
        }
        return $GLOBALS[SP_LOADER]['MODELS'][$modelName];
    }
    /**
     * 装载数据库
     */
    function &database($dbstr,$config = ''){
        if (!isset($GLOBALS[SP_LOADER]['DATABASES'][$dbstr])){
            if(is_array($config)){
                $DB_config = $config;
            }else{
                $Config =& Loader::config();
                $DB_config = $Config['database'][$dbstr];
            }
            if (file_exists(SYSBASE.'Database/DB.php')){
                require_once(SYSBASE.'Database/DB.php');
                
                $GLOBALS[SP_LOADER]['DATABASES'][$dbstr] =& new DB($DB_config);
            }else{
                showError(__T("Class DB is not valid!"));
            }
        }
        return $GLOBALS[SP_LOADER]['DATABASES'][$dbstr];
    }
    /**
     * 装载视图
     */
    function view($tplFile,$isDisplay = true){
        if(!isset($GLOBALS[SP_LOADER]['OBJECTS']['View'])){
            $Config =& Loader::config();
            $viewEngineObj = 'View_'.ucfirst($Config['view_engine']);
            $viewEngineFile = SYSBASE.'Libraries/View/'.ucfirst($Config['view_engine']).'.php';
            require($viewEngineFile);
            $GLOBALS[SP_LOADER]['OBJECTS']['View']=& new $viewEngineObj();
        }

        if($isDisplay){
            $GLOBALS[SP_LOADER]['OBJECTS']['View']->display($tplFile);
            return true;
        }else{
            return $GLOBALS[SP_LOADER]['OBJECTS']['View']->fetch($tplFile);
        }
    }
    /**
     * 装载配置
     */
    function &config($name = 'conf'){
        if ( !isset($GLOBALS[SP_LOADER]['CONFIGS'][$name])){
            if ( ! file_exists(APPBASE."Config/{$name}.php")){
                showError(sprintf(__T("The configuration file %s.php does not exist."),$name));
            }
            require(APPBASE."Config/{$name}.php");

            if ( ! isset($CONFIG) || ! is_array($CONFIG)){
                showError(__T("Your config file does not appear to be formatted correctly."));
            }

            $GLOBALS[SP_LOADER]['CONFIGS'][$name] =& $CONFIG;
        }
        return $GLOBALS[SP_LOADER]['CONFIGS'][$name];
    }
    /**
     * 载入helper
     *
     */
    function helper($helper){
        if(isset($GLOBALS[SP_LOADER]['HELPERS'][$helper])){
            return true;
        }

        $helperPath = SYSBASE.'Helpers/'.$helper.'.php';
        $appHelperPath = APPBASE.'Helpers/'.$helper.'.php';
        
        if(file_exists($appHelperPath)){
            $GLOBALS[SP_LOADER]['HELPERS'][$helper] = true;
            require($appHelperPath);
        }else{
            if(file_exists($helperPath)){
                $GLOBALS[SP_LOADER]['HELPERS'][$helper] = true;
                require($helperPath);
            }else{
                return false;
            }
        }
        return true;
    }
}
